# -*- coding: utf-8 -*-
# created by Alberthch - 陈航  2021/7/15 20:05
# Description:怎样找出一个序列中出现次数最多的元素
# 解决方案：collections.Counter类实现

from collections import Counter

words = [
    'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
    'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
    'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
    'my', 'eyes', "you're", 'under'
]

word_counts = Counter(words)
# Counter对象其实就是一个字典，映射值为出现次数
print(word_counts)
# 返回元组列表
top_three = word_counts.most_common(3)
# print(type(top_three))  # <class 'list'>
print(top_three)
# outputs [('eyes', 8), ('the', 5), ('look', 4)]

# Example of merging in more words，合并更多元素

morewords = ['why', 'are', 'you', 'not', 'looking', 'in', 'my', 'eyes']
word_counts.update(morewords)
print(word_counts)
print(word_counts.most_common(3))
